﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.DataAcsess.Interfaces;
using StructureMap;
using AutoMapper;
using KCMS.BusinessLogic.Models;
using KCMS.BusinessLogic.Interfaces;


namespace KCMS.BusinessLogic.Engine
{
    public class ContactService:IContactService
    {
        private IContactManager contactManager = ObjectFactory.GetInstance<IContactManager>();
        public Models.Contact getById(Guid id)
        {
            DataAcsess.Entitys.ContactEntity contactEntity = contactManager.getById(id);
            return Mapper.Map<DataAcsess.Entitys.ContactEntity, Models.Contact>(contactEntity);
        }
        public Models.Contact getByCode(String code)
        {


            var contactEntity = (from t in contactManager.getAll()
                              where (t.Code.Equals(code))
                              select t).SingleOrDefault();
            return Mapper.Map<DataAcsess.Entitys.ContactEntity, Models.Contact>(contactEntity);
        }

        public IEnumerable<Models.Contact> getAll()
        {
            return Mapper.Map<IEnumerable<DataAcsess.Entitys.ContactEntity>, IEnumerable<Models.Contact>>(contactManager.getAll());

        }

        public void update(Contact contact)
        {
            contactManager.update(Mapper.Map<Contact, DataAcsess.Entitys.ContactEntity>(contact));
        }


        public void save(Contact contact)
        {
            contactManager.save(Mapper.Map<Contact, DataAcsess.Entitys.ContactEntity>(contact));
        }


        public void delete(Guid id)
        {
            contactManager.delete(id);
        }
    }
}
